package com.eascs.web.demo.session;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.site.view.ViewContext;
import com.eascs.web.demo.view.DefaultMasterViewController;

@Controller
public class SessionSampleViewController extends DefaultMasterViewController {
	private final Logger logger = LoggerFactory.getLogger(SessionSampleViewController.class);

	@RequestMapping(value = "/sample/session/", method = RequestMethod.GET)
	public String index(SessionSampleForm cookieSampleForm) {
		String opt = cookieSampleForm.getOpt();
		if (StringUtil.hasText(opt)) {
			if ("remove".equals(opt)) {
				Sessions.USER_ID.remove();;
			} else if ("update".equals(opt)) {
				Sessions.USER_ID.setValue(cookieSampleForm.getValue());
			}
		}
		ViewContext.put("id", Sessions.USER_ID.getValue());
		return "/sample/session/index";
	}

	@Override
	public void preHandle() {
		ViewContext.put("k2", "v2");
		logger.debug("preHandle(..)");
	}

	@Override
	public void afterCompletion() {
		ViewContext.put("k3", "v3");
		logger.debug("afterCompletion(..)");
	}

}
